package com.glela.component;

import com.alibaba.fastjson.JSON;
import com.glela.biz.RedPacketBiz;
import com.glela.domain.dto.GiveRedPacketByEventRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RedPacketListener implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(RedPacketListener.class);

    @Autowired
    private RedPacketBiz redPacketBiz;

    @Override
    public void onMessage(Message message) {

        if (message == null || message.getBody() == null || message.getBody().length == 0) {

            logger.error("---add redpackage --  红包发送报文体为空-----------");

            return;
        }

        String body = new String(message.getBody());

        logger.error("add redpackage --  messageBody={}", body);

        try {

            GiveRedPacketByEventRequest request = JSON.parseObject(body, GiveRedPacketByEventRequest.class);

            if (request == null) {
                logger.error("add redpackage --  ---------JSON字符转换GiveRedPacketByEventRequest对象为空。body={}", body);
                return;
            }
            redPacketBiz.giveRedPacketByEventType(request);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("---------消费红包发送异常。 消息体={}", body, e);

        }

    }
}
